﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUL;
using Entity;

namespace Web6_Sample
{
    public partial class _Default : System.Web.UI.Page
    {
        //AdvertiseBUL bul = new AdvertiseBUL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadAdvertise();
            }
        }

        private void LoadAdvertise()
        {
            grvNguoidung.PageSize = 12;
             grvNguoidung.DataSource = AdvertiseBUL.Advertise_GetByAll("vi");
             grvNguoidung.DataBind();
        }

        protected void ChkAllCheckChanged(object sender, EventArgs e)
        {
            var chkh = (CheckBox)grvNguoidung.HeaderRow.FindControl("ChkAll");
            foreach (GridViewRow row in grvNguoidung.Rows)
            {
                var chk = (CheckBox)row.FindControl("chkItem");
                chk.Checked = chkh.Checked;
            }
        }

        protected void GrvRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvNguoidung.EditIndex = -1;
            LoadAdvertise();
        }

        protected void GrvRowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (grvNguoidung.DataKeys[e.RowIndex] == null)
                return;
            var Id = grvNguoidung.DataKeys[e.RowIndex].Value.ToString();
            var Name = (TextBox)grvNguoidung.Rows[e.RowIndex].FindControl("txtuserName");
            var iemUpdate = AdvertiseBUL.Advertise_GetById(Id);
            iemUpdate.Name = Name.Text;

            AdvertiseBUL.Advertise_Update(iemUpdate);

            grvNguoidung.EditIndex = -1;
            LoadAdvertise();
        }

        protected void GrvRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (grvNguoidung.DataKeys[e.RowIndex] == null)
                return;
            var userId = grvNguoidung.DataKeys[e.RowIndex].Value.ToString();

            AdvertiseBUL.Advertise_Delete(userId);

            LoadAdvertise();
        }

        protected void GrvRowEditing(object sender, GridViewEditEventArgs e)
        {
            grvNguoidung.EditIndex = e.NewEditIndex;
            LoadAdvertise();
         }

        protected void GrvDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
                e.Row.Cells[3].Attributes.Add("onclick", "return confirm('Bạn có chắc chắn xóa bỏ quảng cáo này?')");
        }

        protected void GrvPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvNguoidung.PageIndex = e.NewPageIndex;
            LoadAdvertise();
        }

        //XoaHangChonClick
        protected void XoaHangChonClick(object sender, EventArgs e)
        {
            for (int i = 0; i < grvNguoidung.Rows.Count; i++)
            {
                var cb = (CheckBox)grvNguoidung.Rows[i].FindControl("chkItem");
                if (cb != null && cb.Checked)
                {
                   // listItemDelete.Add((int)grvNguoidung.DataKeys[i].Value);
                }
            }
           
        }
    }
}
   